#include "../base/list.h"
#include <stdio.h>
#include <assert.h>
#define TEST(case_num) \
	test_##case_num
	
	

struct testobj {
	size_t index;
	int value;
};

void TEST(1)() {
	struct list* lists = list_create(sizeof(struct testobj),1);
	struct testobj buffer;
	int i = 0 ;
	for( i = 0 ; i < 500 ; ++i ) {
		buffer.index= i;
		buffer.value= i*i*i;
		list_push_back(lists,&buffer);
	}
	
	assert( list_get_usesize(lists) == 500);
	
	for( i = 0 ; i < 500 ; ++i ) {
		list_pop_back(lists);
	}
	
	assert( list_get_usesize(lists) == 0 );
	
	for( i = 0 ; i < 10 ; ++i ) {
		buffer.index= i;
		buffer.value= i*i*i;
		list_push_front(lists,&buffer);
	}
	
	assert( list_get_usesize(lists) == 10);
	
	for( i =0 ; i < 10; ++i ) {
		list_pop_front(lists);
	}
	
	assert( list_get_usesize(lists) == 0 );
	
	list_destroy(lists);
}





static void print_what_you_know( void* node , void* others ) {
	
	struct testobj * obj = (struct testobj*)(node);
	printf( "index:%d,value:%d\n",obj->index,  obj->value );
}

void TEST(2)() {
	struct list* lists = list_create(sizeof(struct testobj),1);
	struct testobj buffer;
	int i = 0 ;
	for( i = 0 ; i < 500 ; ++i ) {
		buffer.index= i;
		buffer.value= i*i*i;
		list_push_back(lists,&buffer);
	}
	
	list_foreach(lists,print_what_you_know,NULL);
	
	
	// manually foreach 
	
	void* head = list_head(lists);
	void* end = list_tail(lists);
	
	while( head != end ) {
		struct testobj * ob = (struct testobj*)(head);
		printf("index:%d,value:%d\n", ob->index, ob->value);
		head = list_move_list_node(1,head,lists);
	}
	list_destroy(lists);
}


void TEST(3)() {
	struct list* li = list_create( sizeof(int) , 100 ) ;
	int i = 0 ; 
	for( ; i < 1000 ; ++i ) {
		list_push_back(li,&i);
	}
	
	printf("front:%d", *(int*)list_front(li)  );
	printf("end:%d",*(int*)list_back(li) );
	assert( *(int*)list_back(li) == 999 );
	assert( *(int*)list_front(li) == 0 );
	
	list_clear(li);
	list_destroy(li);
}

void test() {
	TEST(1)();
	TEST(2)();
	TEST(3)();
	printf("List test completely!");
}